/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.bus;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.Element;
import twaver.Generator;
import twaver.Link;
import twaver.Node;
import twaver.ShapeNode;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.swing.TableLayout;
import demo.DemoPane;
import demo.DemoUtil;

public class BusDemo extends DemoPane {

	private TDataBox box = new TDataBox();
	private TNetwork network = new TNetwork();
	private BusNetwork busNetwork = null;
	private final static Font font = TWaverUtil.getFont(Font.PLAIN, 11);

	public BusDemo() {
		this.setLayout(new BorderLayout());
		network.setDataBox(box);
		network.setElementSelectColorGenerator(new Generator() {
			public Object generate(Object object) {
				return Color.GREEN;
			}
		});
		network.setTextureBackground("/demo/resource/images/texture.png");
		busNetwork = new BusNetwork(network);

		ShapeNode bus = new ShapeNode();
		bus.setShapeNodeType(TWaverConst.SHAPENODE_ORTHOGONAL_LINE);
		bus.putShapeNodeJointPoint(TWaverConst.JOINT_POINT_SOUTH);
		bus.putCustomDrawOutlineStroke(TWaverConst.STROKE_SOLID_1);
		bus.putCustomDrawOutlineColor(Color.GREEN);
		busNetwork.setBus(bus);
		box.addElement(bus);

		for (int i = 0; i < 20; i++) {
			Node node = new Node();
			node.setName("Server-" + i);
			node.setImage("/demo/resource/images/pc.png");
			node.putLabelColor(Color.WHITE);
			node.putLabelHighlightable(false);
			node.putLabelFont(font);
			node.putBorderStroke(TWaverConst.STROKE_SQUARE_THIN);
			node.putBorderInsets(20);
			node.putBorderType(TWaverConst.BORDER_TYPE_SHAPE);
			node.putBorderShapeFactory(TWaverConst.SHAPE_ROUND_RECTANGLE_QUARTER);
			box.addElement(node);

			Link link = new Link(node, bus);
			link.putLinkWidth(1);
			link.putLinkOutlineWidth(1);
			link.putLinkColor(Color.GREEN);
			link.putLinkOutlineColor(Color.BLUE);
			if (i % 5 == 0) {
				link.putLinkFlowing(true);
				link.putLinkWidth(2);
				link.putLinkOutlineWidth(0);
				link.putLinkFlowingWidth(2);
				link.putLinkFlowingColor(Color.RED);
			}
			box.addElement(link);
		}

		JPanel panel = createControlPanel();
		DemoUtil.createInternalFrame("Control Pane", network, panel);
		this.add(busNetwork);
	}

	public JPanel createControlPanel() {
		JPanel controlPanel = TWaverUtil.createVerticalPanel(2);
		JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 2, 2));
		JButton button = new JButton("Add Element");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int size = box.size();
				Node node = new Node();
				node.setName("Server-" + size);
				node.setImage("/demo/resource/images/pc.png");
				node.putLabelColor(Color.WHITE);
				node.putLabelHighlightable(false);
				node.putLabelFont(font);
				node.putBorderStroke(TWaverConst.STROKE_SQUARE_THIN);
				node.putBorderInsets(20);
				node.putBorderType(TWaverConst.BORDER_TYPE_SHAPE);
				node.putBorderShapeFactory(TWaverConst.SHAPE_ROUND_RECTANGLE_QUARTER);
				box.addElement(node);

				Link link = new Link(node, busNetwork.getBus());
				link.putLinkWidth(1);
				link.putLinkOutlineWidth(1);
				link.putLinkColor(Color.GREEN);
				link.putLinkOutlineColor(Color.BLUE);
				box.addElement(link);
			}
		});
		buttonPanel.add(button);
		button = new JButton("Remove Element");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List list = box.getSelectionModel().getAllSelectedElement();
				int size = list.size();
				for (int i = 0; i < size; i++) {
					Element element = (Element) list.get(i);
					if (element != busNetwork.getBus() && element instanceof Node) {
						box.removeElement(element);
					}
				}
			}
		});
		buttonPanel.add(button);
		controlPanel.add(buttonPanel);

		double[] rows = new double[] { TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED };
		double[] cols = new double[] { TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED };
		JPanel panel = new JPanel(new TableLayout(cols, rows, 2, 2));
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		final JSlider busWidthSlider = new JSlider(50, 800);
		final JTextField busWidthField = new JTextField(3);
		busWidthField.setEditable(false);
		busWidthSlider.setValue((int) busNetwork.getBusWidth());
		busWidthField.setText(busWidthSlider.getValue() + "");
		busWidthSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				busNetwork.setBusWidth(busWidthSlider.getValue());
				busWidthField.setText(busWidthSlider.getValue() + "");
			}
		});
		panel.add(new JLabel("Bus Width"), "0,0,0,0");
		panel.add(busWidthSlider, "1,0,1,0");
		panel.add(busWidthField, "2,0,2,0");

		final JSlider lineHeightSlider = new JSlider(20, 200);
		final JTextField lineHeightField = new JTextField(3);
		lineHeightField.setEditable(false);
		lineHeightSlider.setValue((int) busNetwork.getLineLenght());
		lineHeightField.setText(lineHeightSlider.getValue() + "");
		lineHeightSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				busNetwork.setLineLenght(lineHeightSlider.getValue());
				lineHeightField.setText(lineHeightSlider.getValue() + "");
			}
		});
		panel.add(new JLabel("Line Height"), "0,1,0,1");
		panel.add(lineHeightSlider, "1,1,1,1");
		panel.add(lineHeightField, "2,1,2,1");

		final JSlider busVerticalSpaceSlider = new JSlider(100, 500);
		final JTextField busVerticalSpaceField = new JTextField(3);
		busVerticalSpaceField.setEditable(false);
		busVerticalSpaceSlider.setValue((int) busNetwork.getBusVerticalGap());
		busVerticalSpaceField.setText(busVerticalSpaceSlider.getValue() + "");
		busVerticalSpaceSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				busNetwork.setBusVerticalGap(busVerticalSpaceSlider.getValue());
				busVerticalSpaceField.setText(busVerticalSpaceSlider.getValue() + "");
			}
		});
		panel.add(new JLabel("Bus Vertical Gap"), "0,2,0,2");
		panel.add(busVerticalSpaceSlider, "1,2,1,2");
		panel.add(busVerticalSpaceField, "2,2,2,2");

		final JSlider elementGapSlider = new JSlider(0, 100);
		final JTextField elementGapField = new JTextField(3);
		elementGapField.setEditable(false);
		elementGapSlider.setValue((int) busNetwork.getElementGap());
		elementGapField.setText(elementGapSlider.getValue() + "");
		elementGapSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				busNetwork.setElementGap(elementGapSlider.getValue());
				elementGapField.setText(elementGapSlider.getValue() + "");
			}
		});
		panel.add(new JLabel("Element Gap"), "0,3,0,3");
		panel.add(elementGapSlider, "1,3,1,3");
		panel.add(elementGapField, "2,3,2,3");

		controlPanel.add(panel);
		return controlPanel;
	}
}